package com.chenwc.netty.tcp.config;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.Properties;

/**
 * netty配置实体类
 *
 * @author chenwc
 * @date 2023/5/28 0:52
 */
public class NettyProperties {

    /**
     * boss线程数量
     */
    private Integer boss = 1;

    /**
     * worker线程数量
     */
    private Integer worker = 100;

    /**
     * 连接超时时间
     */
    private Integer timeout = 30;

    /**
     * 服务器主端口
     */
    private Integer port = 9877;

    /**
     * 服务器备用端口
     */
    private Integer portSalve = 7899;

    /**
     * 服务器地址 默认为本地
     */
    private String host = "localhost";

    /**
     * 数据分割符，自定义处理拆包/沾包
     */
    private String delimiterStr = "##@##";

    /**
     * 使用自定义处理拆包/沾包，并且每次查找的最大长度为 maxFrameLength 字节
     */
    private Integer maxFrameLength = 1024;

    public NettyProperties() {
        Properties nettyProperties = new Properties();
        InputStream is = null;
        try {
            is = NettyProperties.class.getClassLoader().getResourceAsStream("netty.properties");
            nettyProperties.load(is);
            this.boss = nettyProperties.containsKey("netty.boss") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.boss")) ? Integer.valueOf(nettyProperties.getProperty("netty.boss")) : this.boss;
            this.worker = nettyProperties.containsKey("netty.worker") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.worker")) ? Integer.valueOf(nettyProperties.getProperty("netty.worker")) : this.worker;
            this.timeout = nettyProperties.containsKey("netty.timeout") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.timeout")) ? Integer.valueOf(nettyProperties.getProperty("netty.timeout")) : this.timeout;
            this.port = nettyProperties.containsKey("netty.port") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.port")) ? Integer.valueOf(nettyProperties.getProperty("netty.port")) : this.port;
            this.portSalve = nettyProperties.containsKey("netty.portSalve") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.portSalve")) ? Integer.valueOf(nettyProperties.getProperty("netty.portSalve")) : this.portSalve;
            this.host = nettyProperties.containsKey("netty.host") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.host")) ? nettyProperties.getProperty("netty.host") : this.host;
            this.delimiterStr = nettyProperties.containsKey("netty.delimiterStr") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.delimiterStr")) ? nettyProperties.getProperty("netty.delimiterStr") : this.delimiterStr;
            this.maxFrameLength = nettyProperties.containsKey("netty.maxFrameLength") && StringUtils.isNotEmpty(nettyProperties.getProperty("netty.maxFrameLength")) ? Integer.valueOf(nettyProperties.getProperty("netty.maxFrameLength")) : this.maxFrameLength;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    public String getDelimiterStr() {
        return delimiterStr;
    }

    public void setDelimiterStr(String delimiterStr) {
        this.delimiterStr = delimiterStr;
    }

    public Integer getMaxFrameLength() {
        return maxFrameLength;
    }

    public void setMaxFrameLength(Integer maxFrameLength) {
        this.maxFrameLength = maxFrameLength;
    }

    public Integer getBoss() {
        return boss;
    }

    public void setBoss(Integer boss) {
        this.boss = boss;
    }

    public Integer getWorker() {
        return worker;
    }

    public void setWorker(Integer worker) {
        this.worker = worker;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Integer getPortSalve() {
        return portSalve;
    }

    public void setPortSalve(Integer portSalve) {
        this.portSalve = portSalve;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}
